// 请求模块
import apis from './libs/http'

// 工具方法
import index from './libs/utils/index.js'
// 路由跳转工具
import router from './libs/router/index.js'
// Md5加密
import md5 from './libs/utils/md5.js'
// 规则检验
import validate from './libs/utils/validate.js'
// 防抖方法
import debounce from './libs/utils/debounce.js'
// 节流方法
import throttle from './libs/utils/throttle.js'
// 设备权限相关方法
import service from './libs/utils/service.js'
// 登录相关方法
import login from './libs/utils/login.js'
// 颜色处理方法
import color from './libs/utils/color.js'
// 文件上传
import upload from './libs/utils/upload.js'
// 弹窗处理
import push from './libs/utils/push.js'
// 弹窗处理
import showModal from './libs/modal/index.js'
// 时间处理
import moment from './libs/moment/index.js'
// 商品规格邻接矩阵
import specs from './libs/specs/index.js'
// 微信关方法
import wechat from './libs/wechat/index.js'
// APP版本升级
import version from './libs/utils/upgrade.js'
// APP版本升级
import { chooseContact } from './libs/utils/contact.js'
// APP分享，使用方法
import share from './libs/share/index.js'
// 小程序分享，使用混入
import shareMixin from './libs/share/mixin.js'
// Composition API混入
import mixin from './libs/mixin/mixin'
// 收货地址解析
import addressParse from './libs/utils/addressParse.js'

const $xl = {
	apis,
	...index,
	router,
	md5,
	validate,
	debounce,
	throttle,
	service,
	upload,
	login,
	color,
	wechat,
	push,
	showModal,
	...share,
	chooseContact,
	addressParse,
	moment,
	specs,
	version,
	mixin,
	store: {},
	setStore(store){
		this.store = store
	},
	global: {},
	setGlobal(options){
		this.global = options
		return options
	},
}

uni.$xl = $xl

const install = (Vue) => {
    // #ifdef H5
	const importFn = import.meta.glob('./components/xl-*/xl-*.vue', { eager: true })
    for (const path in importFn) {
		const module = importFn[path];
		// 判断是否有 default 导出，否则直接使用模块对象
		const component = module?.default || module;
		// 动态生成组件名，例如：u-button.vue -> UButton
		const componentName = path
			.split('/')
			.pop()
			?.replace(/\.\w+$/, '') // 去掉文件扩展名
			.replace(/-([a-z])/g, (_, char) => char.toUpperCase()) // 转换为驼峰
			.replace(/^./, char => char.toUpperCase()); // 首字母大写
		if (componentName) {
			Vue.component(componentName, component);
		} else {
			console.warn(`组件 ${path} 无法生成有效的全局注册名称`);
		}
	}
    // #endif

    // 同时挂载到uni和Vue.prototype中
    // $xl挂载到uni对象上
    uni.$xl = $xl

    // #ifndef APP-NVUE
    // 只有vue，挂载到Vue.prototype才有意义，因为nvue中全局Vue.prototype和Vue.mixin是无效的
    Vue.config.globalProperties.$xl = $xl
	Vue.mixin(mixin)
	Vue.mixin(shareMixin)
    // #endif
	
}

export default {
    install
}
